package com.socool.sinofaith.ui;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.socool.sinofaith.R;
import com.socool.sinofaith.S;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.view.MyDialog;
import com.socool.sinofaith.view.TiPsDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends BaseActivity implements CenterViewFactory, SpinnerViewFactory, View.OnClickListener {

	private FragmentManager fragmentManager;
	ActionBarHelperBase mActionBarHelperBase;
	private RelativeLayout tabRelativeLayout01, tabRelativeLayout02, tabRelativeLayout03, tabRelativeLayout04;
	private ImageView tabImageView01, tabImageView02, tabImageView03, tabImageView04;
	private TiPsDialog mTiPsDialog;

	public static ImageView imageview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SocoolApplication.addActivity(this);
		mTiPsDialog = new TiPsDialog(this, R.style.dialog, null, "离线登录状态下该功能不可用");
		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);
		mActionBarHelperBase.setmClickListener(this);

		imageview = new ImageView(this);

		tabRelativeLayout01 = (RelativeLayout) findViewById(R.id.tabRelativeLayout01);
		tabRelativeLayout02 = (RelativeLayout) findViewById(R.id.tabRelativeLayout02);
		tabRelativeLayout03 = (RelativeLayout) findViewById(R.id.tabRelativeLayout03);
		tabRelativeLayout04 = (RelativeLayout) findViewById(R.id.tabRelativeLayout04);
		tabImageView01 = (ImageView) findViewById(R.id.tabImageView01);
		tabImageView02 = (ImageView) findViewById(R.id.tabImageView02);
		tabImageView03 = (ImageView) findViewById(R.id.tabImageView03);
		tabImageView04 = (ImageView) findViewById(R.id.tabImageView04);
		tabRelativeLayout01.setOnClickListener(this);
		tabRelativeLayout02.setOnClickListener(this);
		tabRelativeLayout03.setOnClickListener(this);
		tabRelativeLayout04.setOnClickListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		S.checkGPS(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			// 具体的操作代码
			MyDialog.Builder builder = new MyDialog.Builder(this);
			builder.setTitle("确定要退出吗？").setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					// MainActivity.this.finish();// 结束当前Activity
					android.os.Process.killProcess(android.os.Process.myPid());
					Intent intent = new Intent(Intent.ACTION_MAIN);
					intent.addCategory(Intent.CATEGORY_HOME);
					startActivity(intent);
					System.exit(0);
				}
			}).setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			Dialog dialog = builder.create();
			dialog.show();

		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public View createView() {

		TextView tv = new TextView(this);
		tv.setText("新诤信");
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		return tv;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated
		int itemId = item.getItemId();
		if (itemId == R.id.home) {
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public View createSpinnerView() {

		imageview.setImageResource(R.drawable.refresh);
		imageview.setPadding(0, 0, 20, 0);
		imageview.setId(10000);
		fragmentManager = this.getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		Fragment fragment = FragmentFactory.getInstanceByIndex(1);
		transaction.replace(R.id.content, fragment);
		transaction.commit();
		return imageview;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tabRelativeLayout01:

			FragmentTransaction transaction = fragmentManager.beginTransaction();
			Fragment fragment = FragmentFactory.getInstanceByIndex(1);
			transaction.replace(R.id.content, fragment);
			transaction.commit();
			tabImageView01.setImageResource(R.drawable.mytask_press);
			tabImageView02.setImageResource(R.drawable.commontasks_normal);
			tabImageView03.setImageResource(R.drawable.question_normal);
			tabImageView04.setImageResource(R.drawable.setting_normal);

			break;
		case R.id.tabRelativeLayout02:
			if (!((SocoolApplication) MainActivity.this.getApplication()).isOffLineLogin()) {
				FragmentTransaction transaction2 = fragmentManager.beginTransaction();
				Fragment fragment2 = FragmentFactory.getInstanceByIndex(2);
				transaction2.replace(R.id.content, fragment2);
				transaction2.commit();
				tabImageView01.setImageResource(R.drawable.mytask_normal);
				tabImageView02.setImageResource(R.drawable.commontasks_press);
				tabImageView03.setImageResource(R.drawable.question_normal);
				tabImageView04.setImageResource(R.drawable.setting_normal);
			} else {
				mTiPsDialog.show();
			}
			break;
		case R.id.tabRelativeLayout03:
			if (!((SocoolApplication) MainActivity.this.getApplication()).isOffLineLogin()) {
				FragmentTransaction transaction3 = fragmentManager.beginTransaction();
				Fragment fragment3 = FragmentFactory.getInstanceByIndex(3);
				transaction3.replace(R.id.content, fragment3);
				transaction3.commit();
				tabImageView01.setImageResource(R.drawable.mytask_normal);
				tabImageView02.setImageResource(R.drawable.commontasks_normal);
				tabImageView03.setImageResource(R.drawable.question_press);
				tabImageView04.setImageResource(R.drawable.setting_normal);
			} else {
				mTiPsDialog.show();
			}
			break;
		case R.id.tabRelativeLayout04:

			FragmentTransaction transaction4 = fragmentManager.beginTransaction();
			Fragment fragment4 = FragmentFactory.getInstanceByIndex(4);
			transaction4.replace(R.id.content, fragment4);
			transaction4.commit();
			tabImageView01.setImageResource(R.drawable.mytask_normal);
			tabImageView02.setImageResource(R.drawable.commontasks_normal);
			tabImageView03.setImageResource(R.drawable.question_normal);
			tabImageView04.setImageResource(R.drawable.setting_press);

			break;
		// case 10000:
		// Toast.makeText(MainActivity.this, "111", Toast.LENGTH_LONG).show();
		// break;
		}

	}
}
